package com.powernode.lambda.stream;

import com.powernode.lambda.Student;

import java.util.stream.Stream;

/**
 * 测试stream相关Api collect收集元素、Collectors工具类
 * collect(()->c,(c,s)->void)创建容器c,给c添加元素
 */
public class Stream09 {
    public static void main(String[] args) {
        Stream<Student> stream = Stream.of(
                new Student("张无忌", 26, "男"),
                new Student("赵敏", 22, "女"),
                new Student("周芷若", 21, "女"),
                new Student("女波斯圣使", 30, "女"),
                new Student("灭绝师太", 30, "女"),
                new Student("张三丰", 100, "男")
        );
//        收集到ArrayList，最后一个参数为空实现
//        ArrayList<Student> collect = stream.collect(() -> new ArrayList<Student>(), (students, e) -> students.add(e), (students1, c) -> {});
//        Collectors.toList()等价
//        System.out.println(stream.collect(Collectors.toList()));
//        System.out.println("collect = " + collect);

//        收集到HashMap，最后一个参数为空实现
//        HashMap<Student, Integer> collect = stream.collect(() -> new HashMap<Student, Integer>(), (map, stu) -> map.put(stu, 1), (students1, c) -> {});
//        Collectors.toMap等价
//        Map<Student, Integer> map = stream.collect(Collectors.toMap(x -> x, y -> 1));
//        for (Map.Entry<Student, Integer> entry : map.entrySet()) {
//            System.out.println(entry);
//        }
//        System.out.println("collect = " + collect);

//        收集到StringBuilder，最后一个参数为空实现
//        StringBuilder collect = stream.collect(() -> new StringBuilder(), (stringBuilder, student) -> {
//            stringBuilder.append(student);
//        }, (students1, c) -> {
//        });
//        System.out.println(collect);
    }

}
